import * as types from "../types/pickerAreaTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";
import { isEmpty } from "../utils/utils";

export function getAreaCode(pickerValue) {
  return dispatch => {
    dispatch({ type: types.PICKER_ALL_LOADING });
    NetWorkUtil.getWithoutLogin(ApiConstants.GETPROVINCE, {})
      .then(provinceList => {
        const provinceName = pickerValue[0];
        const provinceCode = provinceList.find(item => item.name === provinceName).province_code;
        NetWorkUtil.getWithoutLogin(ApiConstants.GETCITY, { province_code: provinceCode })
          .then(cityList => {
            const cityName = pickerValue[1];
            const city = cityList.find(item => item.name === cityName);
            if (!isEmpty(pickerValue[2])) {
              NetWorkUtil.getWithoutLogin(ApiConstants.GETCOUNTY, {
                province_code: provinceCode,
                city_code: city.city_code,
              })
                .then(countryList => {
                  const countryName = pickerValue[2];
                  const country = countryList.find(item => item.name === countryName);

                  dispatch({ type: types.PICKER_ALL_DONE, areaInfo: country });
                })
                .catch(error => {
                  dispatch({ type: types.PICKER_COUNTRY_ERROR, error });
                });
            } else {
              dispatch({ type: types.PICKER_ALL_DONE, areaInfo: city });
            }
          })
          .catch(error => {
            dispatch({ type: types.PICKER_CITY_ERROR, error });
          });
      })
      .catch(error => {
        dispatch({ type: types.PICKER_PROVINCE_ERROR, error });
      });
  };
}

export function initEdit(address) {
  return dispatch => {
    dispatch({
      type: types.PICKER_ALL_DONE,
      areaInfo: address,
    });
  };
}

export function getAllJson() {
  const data = [];
  return dispatch => {
    NetWorkUtil.getWithoutLogin(ApiConstants.GETPROVINCE, {})
      .then(provinceList => {
        for (let x = 0; x < provinceList.length; x += 1) {
          const provinceCode = provinceList[x].province_code;
          const provinceName = provinceList[x].name;
          data.push({ name: provinceName, city: [] });
          NetWorkUtil.getWithoutLogin(ApiConstants.GETCITY, { province_code: provinceCode })
            .then(cityList => {
              for (let y = 0; y < cityList.length; y += 1) {
                const cityCode = cityList[y].city_code;
                const cityName = cityList[y].name;
                data[x].city.push({ name: cityName, area: [] });
                NetWorkUtil.getWithoutLogin(ApiConstants.GETCOUNTY, {
                  province_code: provinceCode,
                  city_code: cityCode,
                })
                  .then(countryList => {
                    for (let z = 0; z < countryList.length; z += 1) {
                      const countryName = countryList[z].name;
                      data[x].city[y].area.push(countryName);
                    }
                    console.log(data);
                    console.log(JSON.stringify(data));
                  })
                  .catch(error => {});
              }
            })
            .catch(error => {});
        }
      })
      .catch(error => {});
  };
}
